package com.hhh.demo.sx.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hhh.demo.sx.bean.User;
import com.hhh.demo.sx.service.UserService;
import com.hhh.demo.sx.util.MD5Util;
import com.hhh.demo.sx.util.ResultMap;
import com.hhh.demo.sx.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author lenovo
 * @create 2022/1/1 14:35
 */
@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserService userService;

    @GetMapping
    public String openLoginPage(){
        SessionUtil.removeCurrentUser();
        return "login";
    }

    //验证用户名，密码是否正确
//    @ResponseBody
    @PostMapping
    public String doLogin(Model model,String username, String password, HttpSession session){
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("username",username)
                .eq("password", MD5Util.md5(password));
        if(userService.getOne(wrapper)!=null){
            User user=userService.getOne(wrapper);
            user.setPassword(null);
            session.setAttribute("user",user);
            System.out.println("到这里了");
            return "index";
        }else{//验证错误
            model.addAttribute("error","账号或密码错误");
            return "login";
        }
    }

}
